package ute.tlcn.struts2hibernate.dao;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	private static StandardServiceRegistry serviceRegistry;

	public static SessionFactory getSesisonFactory() {
		if (sessionFactory == null) {
			Configuration cfg = new Configuration();
			cfg.configure();
			serviceRegistry = new StandardServiceRegistryBuilder()
					.applySettings(cfg.getProperties()).build();
			sessionFactory = cfg.buildSessionFactory(serviceRegistry);
		}
		return sessionFactory;
	}
	
	public static void close() {
		((StandardServiceRegistryImpl)serviceRegistry).destroy();
	}
}
